package com.briup.server.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.common.response.Result;
import com.briup.server.domain.bean.Log;
import com.briup.server.domain.dto.PageQueryDTO;
import com.briup.server.service.LogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/**
 * 操作日志控制器
 */
@Tag(name = "操作日志模块")
@RestController
@RequestMapping("/logs")
@RequiredArgsConstructor
public class LogController {

    private final LogService logService;


    @Operation(summary = "获取操作日志分页列表")
    @GetMapping("/page")
    public Result<Page<Log>> getLogPage(PageQueryDTO pageQueryDTO) {
        // 构建分页对象
        Page<Log> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getSize());
        logService.page(page);
        page.setRecords(
                page.getRecords().stream()
                        .skip((long) (pageQueryDTO.getPage() - 1) * pageQueryDTO.getSize())
                        .limit(pageQueryDTO.getSize())
                        .collect(Collectors.toList())
        );
        page.setTotal(logService.count());
        return Result.success(page)
                .apply("操作日志分页查询成功");
    }
}
